package com.chaofan.websocket.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chaofan.websocket.entity.UserFlowing;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;

public interface UserFlowingMapper extends BaseMapper<UserFlowing> {

    @Select("SELECT IFNULL(sum(if(pay_type=1,end_price-start_price,end_price-start_price)),0) as total FROM admin_user_flowing WHERE room = #{room} AND wexin_id = #{wexinId} AND is_flowing = 1 and  if(DATE_FORMAT(NOW(),'%H')>6,create_time BETWEEN DATE_FORMAT(NOW(),'%Y-%m-%d 05:59:59') and DATE_FORMAT((SELECT DATE_ADD(NOW(),INTERVAL 1 DAY)),'%Y-%m-%d 06:00:00'),create_time BETWEEN DATE_FORMAT((SELECT DATE_ADD(NOW(),INTERVAL -1 DAY)),'%Y-%m-%d 05:59:59') and DATE_FORMAT(now(),'%Y-%m-%d 06:00:00')) ")
    String getOut(UserFlowing userFlowing);

    @Delete("delete FROM admin_user_flowing WHERE room = #{room} AND wexin_id = #{wexinId} AND is_flowing = 1 and period=#{period}")
    int deleteAllFlowingForUser(UserFlowing userFlowing);

    @Select("SELECT IFNULL(sum(pay_price),0) as total FROM admin_user_flowing WHERE room = #{room} AND wexin_id = #{wexinId} AND is_flowing = 1 and pay_type=1 and  if(DATE_FORMAT(NOW(),'%H')>6,create_time BETWEEN DATE_FORMAT(NOW(),'%Y-%m-%d 05:59:59') and DATE_FORMAT((SELECT DATE_ADD(NOW(),INTERVAL 1 DAY)),'%Y-%m-%d 06:00:00'),create_time BETWEEN DATE_FORMAT((SELECT DATE_ADD(NOW(),INTERVAL -1 DAY)),'%Y-%m-%d 05:59:59') and DATE_FORMAT(now(),'%Y-%m-%d 06:00:00')) ")
    String getAll(UserFlowing userFlowing);

    @Select("SELECT count(1) FROM admin_user_flowing WHERE room = #{room} AND wexin_id = #{wexinId} AND is_flowing = 1 and pay_type=1 and  if(DATE_FORMAT(NOW(),'%H')>6,create_time BETWEEN DATE_FORMAT(NOW(),'%Y-%m-%d 05:59:59') and DATE_FORMAT((SELECT DATE_ADD(NOW(),INTERVAL 1 DAY)),'%Y-%m-%d 06:00:00'),create_time BETWEEN DATE_FORMAT((SELECT DATE_ADD(NOW(),INTERVAL -1 DAY)),'%Y-%m-%d 05:59:59') and DATE_FORMAT(now(),'%Y-%m-%d 06:00:00')) ")
    String getAllCount(UserFlowing userFlowing);
}
